package server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

import communication.ClientComm;
import communication.ComandoComm;
import communication.ServerComm;


public class ServerCommSocket implements ServerComm, Runnable {
	private ArrayList<ServerClientCommSocketStream> connections;
	private Thread listener;
	private final int port;
	private Server server;
	
	public ServerCommSocket(int port, Server serv) {
		connections = new ArrayList<ServerClientCommSocketStream>();
		server = serv;
		this.port = port;
		listener = new Thread(this);
		listener.start();
	}
	
	@Override
	public void run() {
		try{
			ServerSocket serverSocket = new ServerSocket(port);
			while(true) {
				Socket socket = serverSocket.accept();
				connections.add(new ServerClientCommSocketStream(socket, server, connections.size()));
			}
		} catch(IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void cambioTurno(String username) {
		for (int i = 0; i<connections.size(); i++){
			connections.get(i).msgToClient(ComandoComm.cambioTurno.toString() + "," + username);
		}
	}
	@Override
	public void connect(ClientComm cl) {}
	@Override
	public void msgFromClient(String msg) {}
	@Override
	public void msgToClient(String msg) {}

	@Override
	public void close() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void closeAll() {
		// TODO Auto-generated method stub
		
	}
}
